package com.yxd.connector.http;

import com.yxd.util.ResponseOutputStream;
import com.yxd.util.StrKit;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * Created by Administrator on 2016/6/16.
 */
public class HttpResponse implements HttpServletResponse {
    private ResponseOutputStream outputStream;
    private HashMap<String, String> header;
    private HashMap<Integer, String> statusMap;
    private int status;
    private HttpRequest request;
    private PrintWriter printWriter;

    public HttpResponse(HttpRequest request, ResponseOutputStream outputStream) {
        this.request = request;
        this.outputStream = outputStream;
        header = new HashMap<>();
        statusMap = new HashMap<>();
        this.setStatus(200, "OK");
        this.setHeader("Server", "yxd tomcat");
        this.setDateHeader("Date", new Date().getTime());
        if (request.getRequestURI().endsWith(".css")) {
            this.setContentType("text/css");
        } else if (request.getRequestURI().endsWith(".js")) {
            this.setContentType("application/x-javascript");
        } else {
            this.setContentType("text/html; charset=utf-8");
        }

//        this.setHeader("Transfer-Encoding", "chunked");
        this.setHeader("Connection", "keep-alive");
        this.setHeader("Vary", "Accept-Encoding");
        this.setCharacterEncoding("utf-8");
        this.setHeader("Cache-Control", "private");
    }

    @Override
    public void addCookie(Cookie cookie) {

    }

    @Override
    public boolean containsHeader(java.lang.String s) {
        return header.containsKey(s);
    }

    @Override
    public java.lang.String encodeURL(java.lang.String s) {
        return null;
    }

    @Override
    public java.lang.String encodeRedirectURL(java.lang.String s) {
        return null;
    }

    @Override
    public java.lang.String encodeUrl(java.lang.String s) {
        return null;
    }

    @Override
    public java.lang.String encodeRedirectUrl(java.lang.String s) {
        return null;
    }

    @Override
    public void sendError(int i, java.lang.String s) throws IOException {

    }


    @Override
    public void sendError(int i) throws IOException {

    }

    @Override
    public void sendRedirect(java.lang.String s) throws IOException {

    }

    @Override
    public void setDateHeader(java.lang.String s, long l) {

    }

    @Override
    public void addDateHeader(java.lang.String s, long l) {

    }

    @Override
    public void setHeader(java.lang.String s, java.lang.String s1) {
        header.put(s, s1);
    }

    @Override
    public void addHeader(java.lang.String s, java.lang.String s1) {
        if (header.containsKey(s)) {
            String value = header.get(s);
            if (StrKit.isWhiteSpaceOrNull(value)) {
                header.put(s, s1);
            } else {
                header.put(s, value + "\r\n" + s1);
            }
        } else {
            header.put(s, s1);
        }
    }

    @Override
    public void setIntHeader(java.lang.String s, int i) {

    }

    @Override
    public void addIntHeader(java.lang.String s, int i) {
    }


    @Override
    public void setStatus(int i) {
        status = i;
    }

    @Override
    public void setStatus(int i, java.lang.String s) {
        setStatus(i);
        statusMap.put(i, s);
    }


    @Override
    public int getStatus() {
        return status;
    }

    @Override
    public java.lang.String getHeader(java.lang.String s) {
        return header.get(s);
    }

    @Override
    public Collection<java.lang.String> getHeaders(java.lang.String s) {
        return null;
    }

    @Override
    public Collection<java.lang.String> getHeaderNames() {
        return header.keySet();
    }


    @Override
    public java.lang.String getCharacterEncoding() {
        return null;
    }

    @Override
    public java.lang.String getContentType() {
        return header.get("Content-Type").toString();
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return outputStream;
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        if (this.printWriter == null) {
            this.printWriter = new PrintWriter(outputStream);
        }
        return this.printWriter;
    }

    @Override
    public void setCharacterEncoding(java.lang.String s) {

    }


    @Override
    public void setContentLength(int i) {

    }

    @Override
    public void setContentType(java.lang.String s) {
        this.setHeader("Content-Type", s);
    }


    @Override
    public void setBufferSize(int i) {

    }

    @Override
    public int getBufferSize() {
        return 0;
    }

    @Override
    public void flushBuffer() throws IOException {

    }

    @Override
    public void resetBuffer() {

    }

    @Override
    public boolean isCommitted() {
        return false;
    }

    @Override
    public void reset() {

    }

    @Override
    public void setLocale(Locale locale) {

    }

    @Override
    public Locale getLocale() {
        return null;
    }


}
